package cn.demomaster.quickrouter.manager;

import static android.content.Context.ACTIVITY_SERVICE;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.RequiresApi;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;

import cn.demomaster.quickrouter.BuildConfig;
import cn.demomaster.quickrouter.lifecycle.LifecycleRecorder;
import cn.demomaster.quickrouter.lifecycle.LifecycleStateType;
import cn.demomaster.quickrouter.quickview.QuickViewManager;

public class QuickActivityLifecycleCallbacks implements Application.ActivityLifecycleCallbacks {

    public static String TAG = QuickActivityLifecycleCallbacks.class.getName();
    /**
     * fragment 生命周期回调
     */
    private FragmentManager.FragmentLifecycleCallbacks sFragmentLifecycleCallbacks;
    QuickActivityLifecycleCallbacks(){
        sFragmentLifecycleCallbacks = new QuickFragmentLifecycleCallbacks();
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        if (activity instanceof FragmentActivity) {
            //注册fragment生命周期回调
            ((FragmentActivity) activity).getSupportFragmentManager().registerFragmentLifecycleCallbacks(sFragmentLifecycleCallbacks, true);
        }
        if (BuildConfig.DEBUG) {
            ActivityManager activityManager = (ActivityManager) activity.getSystemService(ACTIVITY_SERVICE);
            //最大分配内存
            int memory = activityManager.getMemoryClass();
            //最大分配内存获取方法2
            float maxMemory = (float) (Runtime.getRuntime().maxMemory() * 1.0 / (1024 * 1024));
            //当前分配的总内存
            float totalMemory = (float) (Runtime.getRuntime().totalMemory() * 1.0 / (1024 * 1024));
            //剩余内存
            float freeMemory = (float) (Runtime.getRuntime().freeMemory() * 1.0 / (1024 * 1024));
            System.out.println("memory: " + memory + ",maxMemory: " + maxMemory + ",totalMemory: " + totalMemory + ",freeMemory: " + freeMemory);
        }
        QuickActivityManager.getInstance().pushActivity(activity);
        record(LifecycleStateType.onActivityCreated, activity);
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    public void onActivityStarted(Activity activity) {
        QuickViewManager.getInstance().notifyForeground(activity);
        record(LifecycleStateType.onActivityStarted, activity);
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    public void onActivityResumed(Activity activity) {
        //Log.i(TAG,"onActivityResumed="+activity);

        QuickViewManager.getInstance().onActivityResumed(activity);
        QuickActivityManager.getInstance().onActivityResumed(activity);
        record(LifecycleStateType.onActivityResumed, activity);
    }

    @Override
    public void onActivityPaused(Activity activity) {
        QuickActivityManager.getInstance().onActivityPaused(activity);
        record(LifecycleStateType.onActivityPaused, activity);
    }
    
    @Override
    public void onActivityStopped(Activity activity) {
        QuickActivityManager.getInstance().onActivityStopped(activity);
        record(LifecycleStateType.onActivityStopped, activity);
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
        record(LifecycleStateType.onActivitySaveInstanceState, activity);
    }

    @Override
    public void onActivityDestroyed(Activity activity) {
        QuickViewManager.getInstance().onActivityDestroy(activity);
        QuickActivityManager.getInstance().removeActivityFormStack(activity);
        record(LifecycleStateType.onActivityDestroyed, activity);
    }

    private void record(LifecycleStateType lifecycleStateType, Activity activity) {
        log(activity,lifecycleStateType);
        LifecycleRecorder.getInstance().recordEvent(
                activity,
                lifecycleStateType
        );
    }
    private void log(Activity activity, LifecycleStateType lifecycleStateType) {
        Log.i(TAG, String.format("[%s@%d] %s",activity.getClass().getSimpleName(),activity.getClass().hashCode(),lifecycleStateType+""));
    }
}
